<?php

function FH_compare( $element1, $element2 )
{
	return strcasecmp( $element1['FDMS_fh_id'], $element2['FDMS_fh_id']);
}

class FuneralHomes extends Zend_Db_Table_Abstract
{
    protected $_name = 'funeral_homes';
    protected $_rowClass = 'FuneralHome';

    public function getFuneralHomes()
    {
		try {
	        $fetched_rows = $this->fetchAll();
			$results['status'] = 'SUCCEEDED';
			$rows = array();
			foreach( $fetched_rows as $row )
				$rows[] = $row->toArray();			
			$results['rows'] = $rows;
		}
		catch(Exception $e)
		{	
			$results['status'] = 'FAILURE';
			$results['rows'] = array();
		}		
        return $results;
    }

    public function getFuneralHomesForRegion($region)
    {
        if(!isset($region))
		{
			$results['status'] = 'FAILED_BAD_PARAM';
			return $results;	
		} 
		
        if(empty($region))
		{
			$results['status'] = 'FAILED_BAD_PARAM';
			return $results;	
		} 
		
		try {
	        $fetched_rows = $this->fetchAll("region=$region");
			$results['status'] = 'SUCCESS';
			$rows = array();
			foreach( $fetched_rows as $row )
				$rows[] = $row->toArray();			
			$results['rows'] = array( $rows );
		}
		catch(Exception $e)
		{	
			$results['status'] = 'FAILURE';
			$results['rows'] = array();
		}		
        return $results;
    }

    public function getFuneralHomesForArea( $area )
    {
        if(!isset($area) || empty($area))
		{
			$results['status'] = 'FAILED_BAD_PARAM';
			return $results;	
		} 
		
		try {
	        $fetched_rows = $this->fetchAll("area=$area");
			$results['status'] = 'SUCCESS';
			$rows = array();
			foreach( $fetched_rows as $row )
				$rows[] = $row->toArray();			
			$results['rows'] = array( $rows );
		}
		catch(Exception $e)
		{	
			$results['status'] = 'FAILURE';
			$results['rows'] = array();
		}		
        return $results;
    }
		
    public function getProfileForVASId( $VAS_id )
    {
		$results = array();
		
        if(empty($VAS_id)) 
		{
			$results['status'] = 'FAILED_BAD_PARAM';
			return $results;	
		} 

		try {
	        $row = $this->fetchRow("id=$VAS_id");
			if($row)
			{
			$results['status'] = 'SUCCEEDED';
			$results['rows'] = array( $row->toArray() );
			}
			else
			{
			$results['status'] = 'FAILED';
			$results['rows'] = array();
			}
		}
		catch(Exception $e)
		{	
			$results['status'] = 'FAILURE';
			$results['rows'] = array();
		}
				
        return $results;
    }

	public function getFuneralHomesForBranchList( $branch_list )
	{
        if(empty($branch_list))
		{
			$results['status'] = 'FAILED_BAD_PARAM';
			return $results;	
		} 
		
		try {

	        $fetched_rows = $this->fetchAll("branch_number in ($branch_list)");
			$results['status'] = 'SUCCESS';
			$rows = array();
			foreach( $fetched_rows as $row )
				$rows[] = $row->toArray();			
			$results['rows'] = $rows;
		}
		catch(Exception $e)
		{	
			$results['status'] = 'FAILURE';
			$results['rows'] = array();
		}		
        return $results;
	}
	
	public function getHomeByFDMSId( $FDMS_id )
	{		
		$results = array();
		
        if(empty($FDMS_id))
		{
			$results['status'] = 'FAILED_BAD_PARAM';
			return $results;	
		} 

		try {
	        $row = $this->fetchRow("branch_number=$FDMS_id");
			if($row)
			{
				$results['status'] = 'SUCCEEDED';
				$results['rows'] = array( $row->toArray() );
			}
			else
			{
				$results['status'] = 'FAILED';
				$results['rows'] = array( );
			}
		}
		catch(Exception $e)
		{	
			$results['status'] = 'FAILURE';
			$results['rows'] = array();
		}
				
        return $results;
	}
	
	public function getInfoOnTheseBranches( $branches )
	{
		$results = array();
		if(!isset($branches))
		{
			$results['status'] = 'FAILED_BAD_PARAM';
			return $results;	
		}
		
		foreach( $branches as &$branch_number )
			$branch_number = "'$branch_number'";
		$inClause = "(".implode( ",", $branches ).")";
		
		try {
	        $fetched_rows = $this->fetchAll("branch_number IN $inClause");
			$results['status'] = 'SUCCESS';
			$rows = array();
			foreach( $fetched_rows as $row )
				$rows[] = $row->toArray();			
			$results['rows'] = $rows;
		}
		catch(Exception $e)
		{	
			$results['status'] = 'FAILURE';
			$results['rows'] = array();
		}		
		
		return $results;
	}
	
	public function setProfileForVASId( $profile, $VAS_id )
	{		
        if(!isset($VAS_id) || empty($VAS_id))
		{
			$results['status'] = 'FAILED_BAD_PARAM';
			return $results;	
		} 

        if(!isset($profile) || empty($profile))
		{
			$results['status'] = 'FAILED_BAD_PARAM';
			return $results;	
		} 
		
		$fhProfile = array();
		
		$fhProfile['name'] = $profile['name'];
		$fhProfile['addr1'] = $profile['addr1'];
		$fhProfile['city'] = $profile['city'];
		$fhProfile['state'] = $profile['state'];
		$fhProfile['zip'] = $profile['zip'];
		$fhProfile['phone'] = $profile['phone'];
		$fhProfile['manager_name'] = addslashes($profile['manager_name']);
		$fhProfile['region'] = $profile['region'];
		$fhProfile['primary_email'] = $profile['primary_email'];
		
		if(!isset($profile['url'])) $profile['url'] = '';
		$fhProfile['url'] = $profile['url'];
		$fhProfile['show_packages'] = $profile['show_packages'];
		if(!empty($profile['photo_image']))
			$fhProfile['photo_image'] = $profile['photo_image'];
		
		if(!empty($profile['logo_image']))
			$fhProfile['logo_image'] = $profile['logo_image'];
		
		if(empty($profile['modules']))
			$fhProfile['modules'] = VASProcessModules::getDefaultOrder();
		else
			$fhProfile['modules'] = implode("|", $profile['modules']);
			
		try {
	        $r = $this->update($fhProfile, "id=$VAS_id");
			$results['status'] = 'SUCCESS';			
			$results['rows_affected'] = 1;
		}
		catch(Exception $e)
		{	
			$results['status'] = 'FAILURE';
			$results['rows_affected'] = 0;
		}
		
        return $results;
	}
	
	public function getProfileByFDMSId( $branch_number )
	{
        if(!isset($branch_number))
		{
			$results['status'] = 'FAILED_BAD_PARAM';
			return $results;	
		} 
		
		try {
	        $row = $this->fetchRow("branch_number='$branch_number'");
			$results['status'] = 'SUCCESS';
			
			$modules_str = $row->modules;
			$modules = explode( "|", $modules_str );
			$row['modules_list'] = $modules;

			$results['rows'] = array( $row );
		}
		catch(Exception $e)
		{	
			$results['status'] = 'FAILURE';
			$results['rows'] = array();
		}
				
        return $results;
	}
	
	public function getHomeByBranchNumber( $branch_num )
	{
        if(!isset($branch_num))
		{
			$results['status'] = 'FAILED_BAD_PARAM';
			return $results;	
		} 
		
		try {
	        $fetched_row = $this->fetchRow("branch_number='$branch_num'");
			$results['status'] = 'SUCCEEDED';
			
//			$modules_str = $row->modules;
//			$modules = explode( "|", $modules_str );
//			$row['modules_list'] = $modules;

			$results['rows'] = array( $fetched_row->toArray() );
		}
		catch(Exception $e)
		{	
			$results['status'] = 'FAILURE';
			$results['rows'] = array();
		}
				
        return $results;
	}
	
	public function getProfileForBranchNumber( $branch_num )
	{
        if(!isset($branch_num))
		{
			$results['status'] = 'FAILED_BAD_PARAM';
			return $results;	
		} 
		
		try {
	        $fetched_row = $this->fetchRow("branch_number='$branch_num'");
			if($fetched_row)
			{
				$results['status'] = 'SUCCEEDED';
				$results['rows'] = array( $fetched_row->toArray() );
			}
			else
			{
				$results['status'] = 'FAILURE';
				$results['rows'] = array();
			}
		}
		catch(Exception $e)
		{	
			$results['status'] = 'FAILURE';
			$results['rows'] = array();
		}
				
        return $results;
	}
	
	public function setProfileForFDMSId( $profile, $branch_number )
	{
		$results = array();
		
        if(empty($branch_number))
		{
			$results['status'] = 'FAILED_BAD_PARAM';
			return $results;	
		} 

        if(!isset($profile) || empty($profile))
		{
			$results['status'] = 'FAILED_BAD_PARAM';
			return $results;	
		} 
		
		$fhProfile = array();
		
		$fhProfile['name'] = addslashes(stripslashes($profile['name']));
		$fhProfile['addr1'] = addslashes(stripslashes($profile['addr1']));
		$fhProfile['city'] = addslashes(stripslashes($profile['city']));
		$fhProfile['state'] = addslashes(stripslashes($profile['state']));
		$fhProfile['zip'] = addslashes(stripslashes($profile['zip']));
		$fhProfile['phone'] = addslashes(stripslashes($profile['phone']));
		$fhProfile['region'] = addslashes(stripslashes($profile['region']));
		$fhProfile['manager_name'] = addslashes(stripslashes($profile['manager_name']));
		$fhProfile['primary_email'] = addslashes(stripslashes($profile['primary_email']));
		$fhProfile['show_packages'] = addslashes(stripslashes($profile['show_packages']));
		
		if(!isset($profile['url'])) $profile['url'] = '';
		$fhProfile['url'] = $profile['url'];

		if(!empty($profile['photo_image']))
			$fhProfile['photo_image'] = $profile['photo_image'];
		
		if(!empty($profile['logo_image']))
			$fhProfile['logo_image'] = $profile['logo_image'];
		
		if(empty($profile['modules']))
			$fhProfile['modules'] = VASProcessModules::getDefaultOrder();
		else
			$fhProfile['modules'] = implode("|", $profile['modules']);
					
		try {
	        $r = $this->update($fhProfile, "branch_number=$branch_number");
			$results['status'] = 'SUCCESS';			
			$results['rows_affected'] = 1;
		}
		catch(Exception $e)
		{	
			$results['status'] = 'FAILURE';
			$results['rows_affected'] = 0;
			$results['reason'] = "".$e;
		}		
        return $results;
	}

	// sync-specific methods
	public function addFuneralHome( $profile )
	{
		$results = array();
		
        if(!isset($profile))
		{
			$results['status'] = 'FAILED_BAD_PARAM';
			return $results;	
		} 
		
		$fhProfile = array();
	
		// values coming from FDMS into VAS for funeral homes	
		if(isset($profile['name'])) $fhProfile['name'] = $profile['name'];
		if(isset($profile['address'])) $fhProfile['addr1'] = $profile['address'];
		if(isset($profile['city'])) $fhProfile['city'] = $profile['city'];
		if(isset($profile['state'])) $fhProfile['state'] = $profile['state'];
		if(isset($profile['zip'])) $fhProfile['zip'] = $profile['zip'];
		if(isset($profile['phone'])) $fhProfile['phone'] = $profile['phone'];
		if(isset($profile['email'])) $fhProfile['primary_email'] = $profile['email'];
		if(isset($profile['region'])) $fhProfile['region'] = $profile['region'];
		if(isset($profile['area'])) $fhProfile['area'] = $profile['area'];
		
		// values that don't exist in FDMS so they have to be defaulted
		if(empty($profile['modules']))
			$fhProfile['modules'] = VASProcessModules::getDefaultOrder();
		else
			$fhProfile['modules'] = implode("|", $profile['modules']);
			
		$fhProfile['logo_image'] = '';
		$fhProfile['photo_image'] = '';
		
		// value I'm not sure if it comes in from FDMS or not
		$fhProfile['vendors'] = '';
		
		try
		{
			$result = $this->insert( $fhProfile );
			$results['status'] = 'SUCCEEDED';
			$results['rows_affected'] = 1;
		}
		catch(Exception $e)
		{
			$results['status'] = 'FAILED';
			$results['rows_affected'] = 0;
		}
		
		return $results;
	}
	
	public function updateFuneralHome( $profile, $VAS_id )	
	{
		$results = array();
		
        if(!isset($profile))
		{
			$results['status'] = 'FAILED_BAD_PARAM';
			return $results;	
		} 

		if(empty($VAS_id))
		{
			$results['status'] = 'FAILED_BAD_PARAM';
			return $results;	
		}	
			
		$fhProfile = array();
	
		// values coming from FDMS into VAS for funeral homes	
		if(isset($profile['name'])) $fhProfile['name'] = $profile['name'];
		if(isset($profile['address'])) $fhProfile['addr1'] = $profile['address'];
		if(isset($profile['city'])) $fhProfile['city'] = $profile['city'];
		if(isset($profile['state'])) $fhProfile['state'] = $profile['state'];
		if(isset($profile['zip'])) $fhProfile['zip'] = $profile['zip'];
		if(isset($profile['phone'])) $fhProfile['phone'] = $profile['phone'];
		if(isset($profile['email'])) $fhProfile['primary_email'] = $profile['email'];
		if(isset($profile['region'])) $fhProfile['region'] = $profile['region'];
		if(isset($profile['area'])) $fhProfile['area'] = $profile['area'];
				
		// value I'm not sure if it comes in from FDMS or not
		// $fhProfile['vendors'] = '';
		
		try
		{
			$result = $this->update( $fhProfile, "id=$VAS_id" );
			$results['status'] = 'SUCCEEDED';
			$results['rows_affected'] = 1;
		}
		catch(Exception $e)
		{
			$results['status'] = 'FAILED';
			$results['rows_affected'] = 0;
		}
		
		return $results;
	}
	
	public function setPictureImageForFuneralHomeVASId( $picture, $VAS_id )
	{
		$results = array();
		
        if(!isset($picture))
		{
			$results['status'] = 'FAILED_BAD_PARAM';
			return $results;	
		} 

		if(empty($VAS_id))
		{
			$results['status'] = 'FAILED_BAD_PARAM';
			return $results;	
		}	
			
		$fhProfile = array();
		$fhProfile['photo_image'] = $picture;
		
		try
		{
			$rows_affected = $this->update( $fhProfile, "id=$VAS_id" );
			
			$results['status'] = 'SUCCEEDED';
			$results['rows_affected'] = $rows_affected;
		}
		catch(Exception $e)
		{
			$results['status'] = 'FAILED';
			$results['rows_affected'] = 0;
		}
		
		return $results;
	}
	
	public function setLogoImageForFuneralHomeVASId( $logo, $VAS_id )
	{
		$results = array();
		
        if(!isset($logo))
		{
			$results['status'] = 'FAILED_BAD_PARAM';
			return $results;	
		} 

		if(empty($VAS_id))
		{
			$results['status'] = 'FAILED_BAD_PARAM';
			return $results;	
		}	
			
		$fhProfile = array();
		$fhProfile['logo_image'] = $logo;
		
		try
		{
			$rows_affected = $this->update( $fhProfile, "id=$VAS_id" );
			$results['status'] = 'SUCCEEDED';
			$results['rows_affected'] = $rows_affected;
		}
		catch(Exception $e)
		{
			$results['status'] = 'FAILED';
			$results['rows_affected'] = 0;
		}
		
		return $results;
	}
	
public function setPictureImageForFuneralHomeBranchNumber( $picture, $branch_number )
	{
		$results = array();
		
        if(!isset($picture))
		{
			$results['status'] = 'FAILED_BAD_PARAM';
			return $results;	
		} 

		if(empty($branch_number))
		{
			$results['status'] = 'FAILED_BAD_PARAM';
			return $results;	
		}	
			
		$fhProfile = array();
		$fhProfile['photo_image'] = $picture;
		
		try
		{
			$rows_affected = $this->update( $fhProfile, "branch_number=$branch_number" );
			
			$results['status'] = 'SUCCEEDED';
			$results['rows_affected'] = $rows_affected;
		}
		catch(Exception $e)
		{
			$results['status'] = 'FAILED';
			$results['rows_affected'] = 0;
		}
		
		return $results;
	}
	
	public function setLogoImageForFuneralHomeBranchNumber( $logo, $branch_number )
	{
		$results = array();
		
        if(!isset($logo))
		{
			$results['status'] = 'FAILED_BAD_PARAM';
			return $results;	
		} 

		if(empty($branch_number))
		{
			$results['status'] = 'FAILED_BAD_PARAM';
			return $results;	
		}	
			
		$fhProfile = array();
		$fhProfile['logo_image'] = $logo;
		
		try
		{
			$rows_affected = $this->update( $fhProfile, "branch_number=$branch_number" );
			$results['status'] = 'SUCCEEDED';
			$results['rows_affected'] = $rows_affected;
		}
		catch(Exception $e)
		{
			$results['status'] = 'FAILED';
			$results['rows_affected'] = 0;
		}
		
		return $results;
	}
	
	public function getModulesForFuneralHomeVASId( $VAS_id )
	{
		$results = array();
		
        if(empty($VAS_id)) 
		{
			$results['status'] = 'FAILED_BAD_PARAM';
			return $results;	
		} 

		try {
	        $row = $this->fetchRow("id=$VAS_id");
			if(null != $row)
			{
				$results['status'] = 'SUCCEEDED';
				$modules = $row->modules;
				if(empty($modules))
					$modules = VASProcessModules::getDefaultOrder();					
				$results['modules'] = explode( "|", $modules );
			}
			else
			{
				$results['status'] = 'FAILED';
			}
		}
		catch(Exception $e)
		{	
			$results['status'] = 'FAILURE';
			$results['rows'] = array();
		}
		
		return $results;
	}
	
	public function setModulesForFuneralHomeVASId( $modules, $VAS_id )
	{
		$results = array();
		
        if(!isset($modules))
		{
			$results['status'] = 'FAILED_BAD_PARAM';
			return $results;	
		} 

		if(empty($VAS_id))
		{
			$results['status'] = 'FAILED_BAD_PARAM';
			return $results;	
		}	
			
		$fhProfile = array();
		$fhProfile['modules'] = implode( "|", $modules );
		
		try
		{
			$rows_affected = $this->update( $fhProfile, "id=$VAS_id" );
			$results['status'] = 'SUCCEEDED';
			$results['rows_affected'] = $rows_affected;
		}
		catch(Exception $e)
		{
			$results['status'] = 'FAILED';
			$results['rows_affected'] = 0;
		}
		
		return $results;
	}
	
	public function getFuneralHomesInRegion( $region )
	{
		$results = array();
		
		if(!isset($region))
		{
			$results['status'] = 'FAILED_BAD_PARAM';
			return $results;	
		}	

		try {
	        $fetched_rows = $this->fetchAll("region = $region");
			$results['status'] = 'SUCCEEDED';
			$rows = array();
			$branches = array();
			foreach( $fetched_rows as $row )
			{
				$branches[] = $row->branch_number;
			}
			$results['rows'] = $branches;
		}
		catch(Exception $e)
		{	
			$results['status'] = 'FAILURE';
			$results['rows'] = array();
			$results['reason'] = "".$e;
		}
		
        return $results;
	}
}

?>
